import db from '~~/DB';
import { defineResponseResolver } from '~~/mocks/define';
import {} from 'msw';
import { validateToken } from '~~/mocks/middleware/token';

export default validateToken(async (req, res, ctx) => {
  const query: Record<string, any> = {};

  const status = req.url.searchParams.get('status');
  if (status) {
    query.status = Number(status);
  }

  const type = req.url.searchParams.get('type');
  if (type) {
    query.type = Number(type);
  }

  let menus = [];
  if (Object.keys(query).length) {
    menus = await db.menus.where(query).sortBy('sort');
  } else {
    menus = await db.menus.toCollection().sortBy('sort');
  }

  // toCollection().sortBy('sort');
  return res(ctx.json(menus));
});
